package cn.hpclub.server.controller.client.promotion;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hpclub.server.constant.ClientApiConstant;
import cn.hpclub.server.util.InterfaceUtil;
import com.jfinal.plugin.activerecord.Record;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hpclub.server.controller.client.BaseClientController;
import cn.hpclub.server.model.promotion.Promotion;
import cn.hpclub.server.model.promotion.Promotion.Suance;
import cn.hpclub.server.restful.API;
import cn.hpclub.server.util.DateKit;
import cn.hpclub.server.util.ObjectUtil;

/**
 * @FileName: PromotionClientController.java
 * @Author zhl
 * @Description:首页弹窗信息客户端Controller
 * @Date 2016年6月27日 下午2:38:57
 * @CopyRight ZTE Corporation
 */
@API("/promotion/client")
public class PromotionClientController extends BaseClientController<Promotion>{

    private static final Logger logger = LoggerFactory.getLogger(PromotionClientController.class);

    /**
     * 
     * @Title: getPromotion
     * @Description:获取当前有效的最新一条弹窗信息数据
     */
    public void getPromotion(){
        String token = getPara(ClientApiConstant.Key.TOKEN);
        Record userInfoRecord = InterfaceUtil.getuserInfoByToken(token);
        int org_id = userInfoRecord.getInt("org_id");

        Promotion promotion = null;
        List<Object> params = new ArrayList<Object>();
        String systemDateTime = DateKit.toStr(new Date(), "yyyy-MM-dd HH:mm:ss");
        params.add(systemDateTime);
        params.add(systemDateTime);
        params.add(Suance.suance.toString());// 获取已发布的第一条数据
        params.add(org_id);
        String sql = "SELECT * FROM home_page_promotion WHERE start_time <=? AND end_time >? AND issuance=? AND org_id=? AND isdelete=0 order by create_time desc limit 0,1 ";
        List<Promotion> list = promotion.dao.find(sql, params.toArray());
        promotion = ObjectUtil.isNotEmpty(list) && list.size() > 0 ? list.get(0) : null;
        JSONObject jsonObject = setResponseObj(ClientApiConstant.ResultCode.SUCCESS_CODE, ClientApiConstant.Msg.SUCCESS,
                promotion);
        renderJson(jsonObject.toString());
    }
}
